package com.yungu.swift.report.controller;

import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.report.entity.ReportOrderNumDay;
import com.yungu.swift.report.entity.param.AdminOrderNumReportParam;
import com.yungu.swift.report.service.OrderNumDayService;
import com.yungu.swift.report.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 订单量控制器
 */
@Slf4j
@RestController
@RequestMapping("/report/orderNumDay")
@Api(value = "【订单量统计】", tags = "订单量统计")
public class OrderNumDayCtrl {

    @Autowired
    private OrderNumDayService orderNumDayService;

    @PostMapping("/queryIndex")
    @ApiOperation(value = "首页统计——每日订单量", httpMethod = "POST")
    public ResponseData<List<ReportOrderNumDay>> queryIndex(@RequestBody AdminReportParam reportParam) {
        return orderNumDayService.queryIndex(reportParam);
    }

    @PostMapping("/queryPage")
    @ApiOperation(value = "订单量数据查询", httpMethod = "POST")
    public ResponseData<Map<String, Object>> queryPage(@RequestBody AdminReportParam reportParam) {
        return orderNumDayService.queryPage(reportParam);
    }

    @PostMapping("/export")
    @ApiOperation(value = "订单量数据导出", httpMethod = "POST")
    public void export(@RequestBody AdminOrderNumReportParam orderNumReportParam, HttpServletResponse response) throws Exception {
        if (orderNumReportParam.getExportType() == null) {
            log.warn("{} 报表,参数错误", "订单量");

        }
        List<ReportOrderNumDay> list = this.orderNumDayService.list(orderNumReportParam).getData();
        if (CollectionUtils.isEmpty(list)) {
            log.warn("{} 报表,没有统计数据可导出", "订单量");

        }
        switch (orderNumReportParam.getExportType()) {
            case 1:
                ExcelUtil.download(orderNumReportParam, response, list, ReportOrderNumDay.class, "/template/order_type_mun_day_report.xlsx", "时效订单量");
                break;
            case 2:
                ExcelUtil.download(orderNumReportParam, response, list, ReportOrderNumDay.class, "/template/order_business_mun_day_report.xlsx", "业务订单量");
                break;
            case 3:
                ExcelUtil.download(orderNumReportParam, response, list, ReportOrderNumDay.class, "/template/order_join_business_mun_day_report.xlsx", "跨城业务订单量");
                break;
            default:
                break;
        }
    }
}
